#include <dbg.h>

#include <cassert>
using namespace std;

class Solution {
public:
    int expressiveWords(string s, vector<string>& words) {
        int res = 0;
        for (const string& word : words) {
            if (isStretchy(s, word)) {
                ++res;
                dbg(word);
            }
        }
        dbg(res);
        return res;
    }

private:
    bool isStretchy(string s, string word) {
        int sSize = s.size();
        int wSize = word.size();
        if (wSize > sSize) {  // 长度不匹配
            return false;
        }

        int indexS = 0;  // 双指针
        int indexW = 0;
        while (indexS < sSize && indexW < wSize) {
            char sCh = s[indexS];
            char wCh = word[indexW];
            if (sCh != wCh) {  // 字符不匹配
                return false;
            }

            int sCount = 0;  // 统计 s 中连续相同字符的数量
            while (indexS < sSize && s[indexS] == sCh) {
                ++sCount;
                ++indexS;
            }

            int wCount = 0;  // 统计 word 中连续相同字符的数量
            while (indexW < wSize && word[indexW] == wCh) {
                ++wCount;
                ++indexW;
            }

            if (sCount < wCount /* 长度不匹配 */ ||
                (sCount > wCount &&
                 sCount < 3) /* 需要扩张但是扩张后的长度不超过 3 */) {
                return false;
            }
        }
        return indexS == sSize && indexW == wSize;
    }
};

int main() {
    Solution s;

    vector<string> words = {"hello", "hi", "helo"};
    vector<string> words2 = {"zzyy", "zy", "zyy"};
    vector<string> words3 = {"dinnssoo",
                             "ddinso",
                             "ddiinnso",
                             "ddiinnssoo",
                             "ddiinso",
                             "dinsoo",
                             "ddiinsso",
                             "dinssoo",
                             "dinso"};

    assert(s.expressiveWords("heeellooo", words) == 1);
    assert(s.expressiveWords("zzzzzyyyyy", words2) == 3);
    assert(s.expressiveWords("dddiiiinnssssssoooo", words3) == 3);

    return 0;
}